package com.yb.web.goods;

import java.io.IOException;
import java.util.List;

import net.sf.json.JSONArray;

import com.opensymphony.xwork2.ActionContext;
import com.yb.base.web.BaseAction;
import com.yb.bussiness.global.SystemLogService;
import com.yb.bussiness.global.SystemLogServiceImpl;
import com.yb.bussiness.goods.MarketBrandService;
import com.yb.bussiness.goods.MarketBrandServiceImpl;
import com.yb.bussiness.goods.MarketService;
import com.yb.bussiness.goods.MarketServiceImpl;
import com.yb.bussiness.shoper.BrandService;
import com.yb.bussiness.shoper.BrandServiceImpl;
import com.yb.domain.goods.TMarketBrand;
import com.yb.domain.goods.TgMarket;
import com.yb.util.comm.Contants;
import com.yb.util.comm.RequestUtil;
import com.yb.util.framework.MemoryData;
import com.yb.util.framework.PageUtil;

@SuppressWarnings("serial")
public class MarketAction extends BaseAction {
	private TgMarket market;
	private List<Object> rows;
	@SuppressWarnings("unchecked")
	private List list;
	private int total;
	private int page;
	private MarketService service;
	private BrandService brandService;
	private MarketBrandService mbService;
	private Long id;
	private Long marketId;
	private String[] brandId;
	private Long[] mid;
	private Long[] cid;
	private SystemLogService systemLogService;


	public MarketAction() {
		service = new MarketServiceImpl();
		brandService = new BrandServiceImpl();
		mbService = new MarketBrandServiceImpl();
		systemLogService = new SystemLogServiceImpl();
	}

	@SuppressWarnings("unchecked")
	public String showAll() {
		try {
			int limit = RequestUtil.getParam(request, "rows", 20);
			Long cid = RequestUtil.getParam(request, "cid", 0l);
			int count = service.findAll(-1, limit, cid).size();
			total = count;
			list = service.findAll(page, limit, cid);
			rows = list;
		} catch (Exception e) {
			log.error("显示所有市场", e);
		}
		return Contants.JSONSTRING_RESULT_NAME;
	}

	public String showInfo() {
		try {
			ActionContext context = ActionContext.getContext();
			context.put("categoryList", MemoryData.getCategoryList());
			market = service.findById(id);
		} catch (Exception e) {
			log.error("展示市场信息错误", e);
		}
		return "info";
	}

	public String toAddInfo() {
		try {
			ActionContext context = ActionContext.getContext();
			context.put("categoryList", MemoryData.getCategoryList());
		} catch (Exception e) {
			log.error("转向添加市场信息页面", e);
		}
		return "info";
	}

	public String save() {
		try { 
			if(null != cid){
				String categoryStr = "";
				for (int i = 0; i < cid.length; i++) {
					if(i > 0){
						categoryStr += ",";
					}
					categoryStr += cid[i];
				}
				market.setCategoryId(categoryStr);
			}
			if(null==market.getId())
				systemLogService.saveSystemLog(request, "市场管理", "新增市场--"+market.getName());
			 else
				 systemLogService.saveSystemLog(request, "市场管理", "修改市场--"+market.getName());
			service.save(market);
		} catch (Exception e) {
			log.error("市场保存错误", e);
		}
		return "list";
	}

	public String delete() {
		try {
			if (null == market) {
				market = new TgMarket();
				market.setId(id);
			}
			systemLogService.saveSystemLog(request, "市场管理", "删除市场--"+service.findById(Long.valueOf(market.getId())).getName());
			service.deleteMarket(market);
			response.getWriter().print("1");
		} catch (Exception e) {
			log.error("删除市场信息错误", e);
			try {
				response.getWriter().print("2");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}

	public String showAllMarketBrand() {
		try {
			int limit = RequestUtil.getParam(request, "rows", 20);
			list = brandService.findByMarketId(id, page, limit);
			if (null != list && list.size() > 0)
				total = brandService.findByMarketId(id, 1, 1000).size();
			rows = list;
		} catch (Exception e) {
			log.error("显示所有市场旗下品牌出错", e);
		}
		return Contants.JSONSTRING_RESULT_NAME;
	}

	public String showMBList() {
		try {
			market = service.findById(id);
			list = brandService.findByMarketIdNotIn(id, market.getCategoryId(),
					"");
		} catch (Exception e) {
			log.error("显示市场中未有的品牌出错", e);
		}
		return "mbInfo";
	}

	public String saveMB() {
		try {
			if (null != brandId) {
				for (int i = 0; i < brandId.length; i++) {
					TMarketBrand tmb = new TMarketBrand();
					tmb.setBrandId(new Long(brandId[i]));
					tmb.setMarketId(marketId);
					mbService.save(tmb);
				}
			}
		} catch (Exception e) {
			log.error("保存市场品牌出错", e);
		}
		return "marketBrandList";
	}

	public String deleteMB() {
		try {
			TMarketBrand tmb = new TMarketBrand();
			tmb.setBrandId(id);
			tmb.setMarketId(marketId);
			mbService.delete(tmb);
			response.getWriter().print("1");
		} catch (Exception e) {
			log.error("删除市场下品牌出错", e);
			try {
				response.getWriter().print("2");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}

	public String findAllMark() {
		try {

			list = service.findAll();
			response.getWriter().print(JSONArray.fromObject(list));
		} catch (Exception e) {
			log.error("显示所有市场", e);
			try {
				response.getWriter().print("[]");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}

	public String loadByCid() {
		try {
			Long cid = RequestUtil.getParam(request, "cid", 0l);
			list = service.findAllByCid(cid);
			response.getWriter().print(JSONArray.fromObject(list));
		} catch (Exception e) {
			log.error("显示所有市场", e);
			try {
				response.getWriter().print("[]");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}

	public String showMarketByBid() {
		try {
			int limit = RequestUtil.getParam(request, "rows", 20);
			Long bid = RequestUtil.getParam(request, "id", 0l);
			PageUtil pu = service.findAllByBid(bid, page, limit);
			total = pu.getTotalRecords();
			rows = (List<Object>) pu.getList();
		} catch (Exception e) {
			log.error("通过品牌id查找与该品牌相关的市场出错", e);
		}
		return Contants.JSONSTRING_RESULT_NAME;
	}

	public String showBrandMarketInfo() {
		try {
			ActionContext context = ActionContext.getContext();
			context.put("categoryList", MemoryData.getCategoryList());
		} catch (Exception e) {
			log.error("转向添加市场信息页面", e);
		}
		return "showBrandMarketInfo";
	}

	public String saveMarketBrand() {
		try {
			if (null != mid) {
				for (int i = 0; i < mid.length; i++) {
					Long marketId = mid[i];
					TMarketBrand tmb = new TMarketBrand();
					tmb.setBrandId(id);
					tmb.setMarketId(marketId);
					mbService.delete(tmb);
					mbService.save(tmb);
				}
			}
		} catch (Exception e) {
			log.error("添加品牌市场出错", e);
		}
		return "brandMarketList";
	}

	public String showlist() {
		try {
			ActionContext context = ActionContext.getContext();
			context.put("categoryList", MemoryData.getCategoryList());
		} catch (Exception e) {
			log.error("显示市场列表出错", e);
		}
		return "showlist";
	}

	public TgMarket getMarket() {
		return market;
	}

	public void setMarket(TgMarket market) {
		this.market = market;
	}

	public List<Object> getRows() {
		return rows;
	}

	public void setRows(List<Object> rows) {
		this.rows = rows;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	@SuppressWarnings("unchecked")
	public List getList() {
		return list;
	}

	@SuppressWarnings("unchecked")
	public void setList(List list) {
		this.list = list;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getMarketId() {
		return marketId;
	}

	public void setMarketId(Long marketId) {
		this.marketId = marketId;
	}

	public String[] getBrandId() {
		return brandId;
	}

	public void setBrandId(String[] brandId) {
		this.brandId = brandId;
	}

	public Long[] getMid() {
		return mid;
	}

	public void setMid(Long[] mid) {
		this.mid = mid;
	}

	public Long[] getCid() {
		return cid;
	}

	public void setCid(Long[] cid) {
		this.cid = cid;
	}

}
